using System;
using System.Collections.Generic;
using System.Text;

namespace Mitosoft.Definitions.DateTime {

  public static class DateTimeHelper {

    /// <summary>
    /// Checks if the given string is formatted
    /// like HH:MM, where HH represents the hours
    /// and MM the minutes.
    /// </summary>
    /// <param name="valueToCheck">String to be checked</param>
    /// <returns>true if the HH:MM format is given,
    /// otherwise false</returns>
    public static bool IsHHMM(String valueToCheck) {

      // the lenght must be 5:
      if (valueToCheck.Length != 5) return false;

      // the third character must be a ":"
      if (valueToCheck.Substring(2, 1) != ":") return false;

      // the first two characters AND (the fourth and fifth) have to be between "00" and "59":
      bool hourAndMinuteNumberIsOkay = true;
      try {
        int hourNumber = System.Convert.ToInt16(valueToCheck.Substring(0, 2));
        if (hourNumber < 0 || hourNumber > 59) hourAndMinuteNumberIsOkay = false;
        int minuteNumber = System.Convert.ToInt16(valueToCheck.Substring(3, 2));
        if (minuteNumber < 0 || minuteNumber > 59) hourAndMinuteNumberIsOkay = false;
      } catch {
        hourAndMinuteNumberIsOkay = false;
      }

      return hourAndMinuteNumberIsOkay;

    }

  }
}
